package scau.zhong.service.Impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.oauth2.provider.ClientDetails;
import org.springframework.security.oauth2.provider.ClientDetailsService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import scau.zhong.mapper.RoleMapper;
import scau.zhong.mapper.UserMapper;
import scau.zhong.pojo.Permission;
import scau.zhong.pojo.Role;
import scau.zhong.pojo.User;

import java.util.List;

@Service
@Transactional
public class UserService implements scau.zhong.service.UserService {



    @Autowired
    private UserMapper userMapper;

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private BCryptPasswordEncoder encoder;

    @Autowired
    private ClientDetailsService clientDetailsService;

    @Override
    public List<User> findAll() {
        return userMapper.findAll();
    }


    /**
     * 登录认证
     * @param s
     * @return
     * @throws UsernameNotFoundException
     */
    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
        //oauth2密码模式，认证client_id与secret
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        //如果采用了httpbatic认证，开始认证client
        if(authentication == null){
            ClientDetails clientDetails = clientDetailsService.loadClientByClientId(s);
            if(clientDetails !=null){
                String secret = clientDetails.getClientSecret();
                User user = new User();
                user.setUsername(s);
                user.setPassword(secret);
                return user;
            }
        }


        User byUsername = userMapper.findByUsername(s);
        System.out.println(byUsername);
        return byUsername;
    }

    /**
     * 根据用户ID查询用户权限
     * @param id
     * @return
     */
    @Override
    public List<Role> queryRoleById(String id) {
        return userMapper.queryRoleByUID(id);
    }

    /**
     * 根据用户ID查询用户角色
     * @param id
     * @return
     */
    @Override
    public List<Permission> queryPermissionByUID(String id) {
        return userMapper.queryPermissionByUID(id);
    }

    @Override
    public int insertUser(User user) {
        int flag1 = userMapper.insert(user);
        String uid = user.getId();
        String rid = "1"; // 1是用户 2是管理员，直接指定了，省得在查一次表
        String pid = "1"; // 1是客户端权限 2是管理后台权限
        int flag2 = userMapper.insertUser_Role(uid,rid);
        System.out.println(flag1+flag2+"");
        return flag1 & flag2 ;
    }

    @Override
    public int insertAdmin(User user) {
        int flag1 = userMapper.insert(user);
        String uid = user.getId();
        String rid = "2"; // 1是用户 2是管理员，直接指定了，省得在查一次表
        int flag2 = userMapper.insertUser_Role(uid,rid);
        return flag1 & flag2 ;
    }

    @Override
    public boolean isUserExit(String email) {
        return !(userMapper.UserCountByEmail(email) == 0);
    }


    public String encode(String pwd){
        return encoder.encode(pwd);
    }

}
